package com.sky.controller.dish;

import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author iTaotao
 * @date 2023/7/28 12:57
 * @desciption: 菜品控制器
 */
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 选择页面
     *
     * @param categoryId 类别id
     * @param name       名字
     * @param page       页面
     * @param pageSize   页面大小
     * @param status     状态
     * @return {@link Result}<{@link ?}>
     */
    @GetMapping("/page")

    public Result<PageResult> selectPage(Long categoryId, String name, @RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pageSize, Integer status) {
        PageResult pageResult = dishService.selectPage(categoryId, name, page, pageSize, status);
        return Result.success(pageResult);

    }

    /**
     * 创建菜
     *
     * @param
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> createDish(@RequestBody DishDTO dishDTO) {

        dishService.createDish(dishDTO);
//        String key = "dish_" + dishDTO.getCategoryId();
//        redisTemplate.delete(key);
        cleanCache("dish_" +dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * 删除盘通过id
     *
     * @param ids id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping
    public Result<?> removeDishById(@RequestParam List<Long> ids) {

        dishService.removeDishById(ids);

//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 通过id查询菜
     *
     * @param id id
     * @return {@link Result}<{@link DishVO}>
     */
    @GetMapping("/{id}")
    public Result<DishVO> queryDishById(@PathVariable Long id) {
        DishVO dishVO = dishService.queryDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 更新菜通过id
     *
     * @param dishDTO 菜dto
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping()
    public Result<?> updateDishById(@RequestBody DishDTO dishDTO) {
        dishService.updateDishById(dishDTO);

//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 更新状态通过id
     *
     * @param status 状态
     * @param id     id
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/status/{status}")
    public Result<?> updateStatusById(@PathVariable Integer status, Long id) {
        dishService.updateStatusById(status, id);

//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        cleanCache("dish_*");
        return Result.success();
    }


    /**
     * 选择列表
     *
     * @param categoryId 类别id
     * @return {@link Result}<{@link List}<{@link Dish}>>
     */
    @GetMapping("/list")
    public Result<List<Dish>> selectList(Long categoryId) {
        List<Dish> dishList = dishService.selectList(categoryId);
        return Result.success(dishList);
    }

    /**
     *
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);

    }
}
